const fs = require('fs');

function calculatePredictPrice(price, condition, matetial) {
  let coefficient = 0;
  switch (condition) {
    case 'S':
      coefficient = 0.8;
      break;
    case 'A':
      coefficient = 0.65;
      break;
    case 'B':
      coefficient = 0.5;
      break;
    case 'C':
      coefficient = 0.3;
      break;
  }
  if (matetial.indexOf('牛皮') >= 0) {
    coefficient += 0.03;
  }
  if (price < 5000) {
    coefficient += 0.02;
  } else if (price < 10000) {
    coefficient += 0.01;
  } else if (price < 15000) {
    coefficient -= 0.01;
  } else {
    coefficient -= 0.02;
  }
  const max = price * 0.05;
  const min = -price * 0.05;
  const random = parseInt(Math.random() * (max - min + 1) + min, 10);
  let predictPrice = parseInt(price * coefficient + random);
  if (predictPrice >= price) {
    predictPrice = price * coefficient - random;
  }
  return { predictPrice, random };
}

function main() {
  const products = require('./prcts.json');
  let dataJson = [];
  let dataStr = [];
  dataStr.push(
    'condition\toriginalPrice\tbaseSalePrice\tpredictPrice\tmockPrice\trandom\tcolor\tmaterial\tsize\tlimitedEdition'
  );
  let priceStr = [];
  let priceStrForS = [];
  let priceStrForA = [];
  let priceStrForB = [];
  let priceStrForC = [];
  for (const obj of products) {
    if (!obj || !obj.fields) continue;
    const product = obj.fields;
    const id = product.id;
    const condition = product.condition;
    const originalPrice = product.originalPrice || 0;
    const baseSalePrice = product.baseSalePrice || 0;
    const color =
      product.color && product.color.name ? product.color.name : 'NULL';
    const material =
      product.material && product.material.name
        ? product.material.name
        : 'NULL';
    const size = product.size && product.size.name ? product.size.name : 'NULL';
    const limitedEdition = product.limitedEdition ? true : false;
    let { predictPrice, random } = calculatePredictPrice(
      originalPrice,
      condition,
      material
    );
    let mockPrice =
      Math.abs(predictPrice - baseSalePrice) > originalPrice * 0.1
        ? predictPrice
        : baseSalePrice;

    if (originalPrice < 20000) {
      dataStr.push(
        `${condition}\t${originalPrice}\t${baseSalePrice}\t${predictPrice}\t${mockPrice}\t${random}\t${color}\t${material}\t${size.replace(
          /\s*/g,
          ''
        )}\t${limitedEdition}`
      );
    }

    if (parseInt(originalPrice) > 50000) {
    } else {
      switch (condition) {
        case 'S':
          priceStrForS.push(`${originalPrice}\t${baseSalePrice}`);
          break;
        case 'A':
          priceStrForA.push(`${originalPrice}\t${baseSalePrice}`);
          break;
        case 'B':
          priceStrForB.push(`${originalPrice}\t${baseSalePrice}`);
          break;
        case 'C':
          priceStrForC.push(`${originalPrice}\t${baseSalePrice}`);
          break;
      }
    }
    dataJson.push({
      id,
      condition,
      originalPrice,
      baseSalePrice,
      color,
      material,
      size,
      limitedEdition,
    });
  }
  fs.writeFileSync('data.json', JSON.stringify(dataJson));
  fs.writeFileSync('data.txt', dataStr.join('\n'));
  fs.writeFileSync('price-s.txt', priceStrForS.join('\n'));
  fs.writeFileSync('price-a.txt', priceStrForA.join('\n'));
  fs.writeFileSync('price-b.txt', priceStrForB.join('\n'));
  fs.writeFileSync('price-c.txt', priceStrForC.join('\n'));
}

main();
